package cn.itlaobing.eshop.web.servlet;

import cn.itlaobing.eshop.entity.Userinfo;
import cn.itlaobing.eshop.exception.BusinessException;
import cn.itlaobing.eshop.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description:
 * @Author: WangDong
 * @Date: 2017/11/14 21:49
 */
@WebServlet("/userservlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("utf-8");
        String account=req.getParameter("account");
        String password=req.getParameter("password");
        UserService userService=new UserService();
        Userinfo userinfo=null;
        try {
            userinfo=userService.findByUser(account,password);
            if(userinfo==null){
                req.setAttribute("MSG","用户名或密码错误！");
                req.getRequestDispatcher("/login.jsp").forward(req,resp);
                return;
            }
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        req.getSession().setAttribute("USER",userinfo);
        resp.sendRedirect(req.getContextPath()+"/index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
